/*
 * ActivityMessageProvider.java
 * business
 *
 * Created by chentao on 2017/4/26.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.recyclerview.provider;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.data.bean.Message;
import com.yidiandao.business.ui.orderabout.activeorder.detail.ActiveOrderDetailActivity;
import com.yidiandao.business.ui.url.UrlActivity;
import com.yidiandao.business.utils.ImageLoader;
import com.yidiandao.business.utils.TextMeasureUtils;
import com.yidiandao.business.utils.TextRenderUtils;
import com.yidiandao.business.utils.UIUtils;

import me.drakeet.multitype.ItemViewProvider;

/**
 * 个人-活动消息 Provider
 * <p>
 * Created by ChenTao(chentao7v@gmail.com) on 2017/4/26 0026 9:49
 */
public class DiscoveryActivityMessageProvider extends ItemViewProvider<Message, DiscoveryActivityMessageProvider.ViewHolder> {

    private Activity mActivity;

    public DiscoveryActivityMessageProvider(Activity activity) {
        mActivity = activity;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_activity_message, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull final Message message) {

        ImageLoader.loadImageWithPlaceHolder(mActivity, holder.ivImage, message.getArticleThumb());

        holder.tvTitle.setText(message.getTitle());

        if (Message.Type.ACTIVITY_ORDER.equals(message.getType())) {
            // 处理报名
            processEnroll(holder, message);
        } else {
            // 处理文章
            processArticle(holder, message);
        }
    }

    private void processArticle(final ViewHolder holder, final Message message) {
        holder.tvContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                final String more = UIUtils.getString(R.string.show);

                String originText = message.getContent();
                int widgetWidth = holder.tvContent.getWidth();

                // 获取截取后的文本
                String correctText = TextMeasureUtils.getCorrectText(originText, more, holder.tvContent.getPaint(), widgetWidth);
                // 最终显示效果
                SpannableStringBuilder builder = TextRenderUtils.getActivityHighlightContent(correctText, more, !message.isRead());
                holder.tvContent.setText(builder);

                holder.tvContent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 去文章详情页面
                UrlActivity.actionStartForArticle(mActivity, message.getArticleId());
            }
        });
    }

    private void processEnroll(@NonNull final ViewHolder holder, @NonNull final Message message) {
        if (!message.isPay()) {
            // 未支付的情况,显示 去支付
            holder.tvContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {

                    final String more = UIUtils.getString(R.string.go_to_pay);

                    String originText = message.getContent();
                    int widgetWidth = holder.tvContent.getWidth();

                    // 获取截取后的文本
                    String correctText = TextMeasureUtils.getCorrectText(originText, more, holder.tvContent.getPaint(), widgetWidth);
                    // 最终显示效果
                    SpannableStringBuilder builder = TextRenderUtils.getActivityHighlightContent(correctText, more, !message.isRead());
                    holder.tvContent.setText(builder);

                    holder.tvContent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            });

        } else {
            // 已支付的情况,就是活动已经报名成功了,直接显示内容
            holder.tvContent.setText(message.getContent());
        }

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 设置消息已读
                // 去活动详情页面
                ActiveOrderDetailActivity.actionStart(mActivity, message.getArticleId());
            }
        });
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private ImageView ivImage;

        private TextView tvTitle;

        private TextView tvContent;

        public ViewHolder(View itemView) {
            super(itemView);
            ivImage = (ImageView) itemView.findViewById(R.id.iv_image);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
            tvContent = (TextView) itemView.findViewById(R.id.tv_content);
        }
    }
}
